package cn.kgm.makeGodV2.relationship;

import cn.kgm.makeGodV2.emotion.PersonalityTrait;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-10-13 13:17
 * @Description: cn.kgm.makeGodV2.relationship.UnidirectionalRelationship
 */
public class UnidirectionalRelationship {
    // 强度阈值常量
    public static final int MAX_INTENSITY = 100;
    public static final int MIN_INTENSITY = -100;
    private final String sourceId;     // 来源角色ID
    private final String targetId;     // 目标角色ID
    private RelationshipType type;     // 关系类型
    private int intensity;             // 关系强度 (-100 到 100)

    public UnidirectionalRelationship(String sourceId, String targetId,
                                      RelationshipType type, int intensity) {
        this.sourceId = sourceId;
        this.targetId = targetId;
        this.type = type;
        this.intensity = clampIntensity(intensity);
    }

    private int clampIntensity(int intensity) {
        return Math.max(MIN_INTENSITY, Math.min(MAX_INTENSITY, intensity));
    }

    /**
     * 根据强度自动更新关系类型
     */
    public void updateTypeFromIntensity() {
        if (intensity >= 80) {
            type = RelationshipType.LOVER;
        } else if (intensity >= 60) {
            type = RelationshipType.ADMIRED;
        } else if (intensity >= 40) {
            type = RelationshipType.CLOSE_FRIEND;
        } else if (intensity >= 20) {
            type = RelationshipType.FRIEND;
        } else if (intensity >= 0) {
            type = RelationshipType.ACQUAINTANCE;
        } else if (intensity >= -20) {
            type = RelationshipType.STRANGER;
        } else if (intensity >= -40) {
            type = RelationshipType.RIVAL;
        } else if (intensity >= -60) {
            type = RelationshipType.ENEMY;
        } else {
            type = RelationshipType.SWORN_ENEMY;
        }
    }

    /**
     * 改变关系强度
     */
    public void changeIntensity(int delta, PersonalityTrait personality) {
        // 应用性格调制
        double modifier = calculatePersonalityModifier(delta, personality);
        int adjustedDelta = (int) (delta * modifier);

        this.intensity = clampIntensity(this.intensity + adjustedDelta);
        updateTypeFromIntensity();
    }

    /**
     * 计算性格对情感变化的调制系数
     */
    private double calculatePersonalityModifier(int delta, PersonalityTrait personality) {
        double modifier = 1.0;

        if (delta > 0) {
            // 正面变化 - 随和性高的人更容易产生好感
            modifier *= (1.0 + personality.getAgreeableness() * 0.005);
        } else {
            // 负面变化 - 神经质高的人更容易记仇
            modifier *= (1.0 + personality.getNeuroticism() * 0.008);
            // 随和性高的人不容易产生恶感
            modifier *= (1.0 - personality.getAgreeableness() * 0.004);
        }

        return Math.max(0.3, Math.min(2.0, modifier));
    }

    // Getter方法
    public String getSourceId() {
        return sourceId;
    }

    public String getTargetId() {
        return targetId;
    }

    public RelationshipType getType() {
        return type;
    }

    public int getIntensity() {
        return intensity;
    }

    @Override
    public String toString() {
        return String.format("%s -> %s: %s (强度: %d)",
                sourceId, targetId, type.getDisplayName(), intensity);
    }
}
